import json
from datetime import datetime

from django.core.paginator import Paginator
from django.http import JsonResponse

from lapi_app.models.testcase_model.testcase import Parametric, Parametric_result


def get_parametric(request):
    try:
        data = json.loads(request.body)
        t_id = data["testcase_id"]
        parametric_list = []
        pas = Parametric.objects.filter(p_testcase_id=t_id)
        size_page = data["size_page"]
        current_pages = data["current_page"]
        parametric_total = len(pas)
        for p in pas:
            p_dict = {
                "id": p.id,
                "file_name": p.file_name,
                "file_content": p.file_content,
                "update_time": datetime.strftime(p.update_time, "%Y-%m-%d %H:%M:%S"),
                "username": p.p_user.username,
                "parametric_type": p.parametric_type
            }
            parametric_list.append(p_dict)
        p = Paginator(parametric_list, size_page)
        page1 = p.page(current_pages)
        parametric_list = page1.object_list
        return JsonResponse({
            "code": 200,
            "message": "获取参数化列表成功",
            "parametric": parametric_list,
            "parametric_total": parametric_total,
            "size_page": size_page,
            "current_page": current_pages
        })
    except Exception as e:
        return JsonResponse({
            "code": 100,
            "message": str(e),
        })


def upload_parametric(request):
    data = json.loads(request.body)
    t_id = data["testcase_id"]
    parametric_type = data["parametric_type"]
    filename = data["filename"]
    upload_data = data["upload_data"]
    user_id = data["user_id"]
    if t_id is None or filename is None or upload_data is None or user_id is None:
        return JsonResponse({
            "code": 100,
            "message": "上传参数化列表失败，规定参数不可为空"
        })
    else:
        try:
            if Parametric.objects.filter(p_testcase_id=t_id, parametric_type=parametric_type).count() > 0:
                ps = Parametric.objects.filter(p_testcase_id=t_id, parametric_type=parametric_type)
                for p in ps:
                    id = p.id
                Parametric.objects.filter(p_testcase_id=t_id, id=id, parametric_type=parametric_type).update(
                    file_name=filename,
                    file_content=upload_data, parametric_type=parametric_type, p_testcase_id=t_id,
                    p_user_id=user_id)
            else:
                Parametric.objects.create(file_name=filename,
                                          file_content=upload_data, parametric_type=parametric_type, p_testcase_id=t_id,
                                          p_user_id=user_id)

            return JsonResponse({
                "code": 200,
                "message": "上传参数化列表成功",
                "testcase_id": t_id,
                "content": data
            })
        except Exception as e:
            return JsonResponse({
                "code": 100,
                "message": str(e)
            })


def del_parametric(request):
    data = json.loads(request.body)
    id = data["parametric_id"]
    if id is None:
        return JsonResponse({
            "code": 100,
            "message": "删除失败，规定参数不可为空",
        })
    else:
        try:
            Parametric.objects.filter(id=id).delete()
            return JsonResponse({
                "code": 200,
                "message": "删除成功",
            })
        except Exception as e:
            return JsonResponse({
                "code:": 100,
                "message": str(e)
            })


def edit_parametric(request):
    data = json.loads(request.body)
    id = data["parametric_id"]
    file_content = data["file_content"]
    if id is None or not file_content:
        return JsonResponse({
            "code": 100,
            "message": "编辑失败，规定参数不可为空",
        })
    else:
        try:
            Parametric.objects.filter(id=id).update(file_content=file_content)
            return JsonResponse({
                "code": 200,
                "message": "编辑参数化数据成功"
            })
        except Exception as e:
            return JsonResponse({
                "code": 100,
                "message": str(e)
            })
